package com.gowow.gowoworionapp.network.response;

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import com.google.gson.JsonSyntaxException;
import com.gowow.gowoworionapp.BuildConfig;
import com.gowow.gowoworionapp.R;
import com.gowow.gowoworionapp.app.GowowOrionApplication;

import java.net.SocketTimeoutException;
import io.reactivex.observers.DisposableSingleObserver;
import retrofit2.HttpException;

public abstract class AbstractDisposableCallBack<T> extends DisposableSingleObserver<T> {
    @Override
    public void onSuccess(@NonNull T e) {
        if (e instanceof HttpException) {
            onHttpError(((HttpException) e));
        } else if (e instanceof SocketTimeoutException) {
            onTimeOutError(((SocketTimeoutException) e));
        } else if (e instanceof JsonSyntaxException) {
            onFailed(HttpErrorCode.JSON_ERROR, R.string.retry_later);
        } else {
            onFailed(HttpErrorCode.ERROR, R.string.retry_later);
        }
    }

    @Override
    public void onError(@NonNull Throwable e) {
        if (e instanceof HttpException) {
            onHttpError(((HttpException) e));
        } else if (e instanceof SocketTimeoutException) {
            onTimeOutError(((SocketTimeoutException) e));
        } else if (e instanceof JsonSyntaxException) {
            onFailed(HttpErrorCode.JSON_ERROR, R.string.retry_later);
        } else {
            onFailed(HttpErrorCode.ERROR, R.string.retry_later);
        }
    }

    protected abstract void onSafeFailed(String code, String message);


//    private void onIOError(IOException e) {
//        //判断是否有网
//        if (Network.isNetWorkAvailable(GlobalContext.getAppContext())) {
//            onFailed(HttpErrorCode.OTHER_IO, e.getMessage());
//        } else {
//            onFailed(HttpErrorCode.NET_ERROR, R.string.network_error);
//        }
//    }

    //    public static NetworkInfo getSystemNetworkInfo(Context context) {
//        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//        NetworkInfo ni = cm.getActiveNetworkInfo();
//        return ni;
//    }
    private void onTimeOutError(SocketTimeoutException e) {
        onFailed(HttpErrorCode.ERROR, e.getMessage());
    }

    private void onFailed(String code, @StringRes int messageId) {
        String message = "";
        if (GowowOrionApplication.Companion.getContext() != null) {
            message = GowowOrionApplication.Companion.getContext().getString(messageId);
        }
        onFailed(code, message);
    }

    private void onHttpError(HttpException error) {
        // 当无缓存时okhttp读取cache会抛出异常：retrofit2.adapter.rxjava2.HttpException: HTTP 504 Unsatisfiable Request (only-if-cached)
        int code = error.code();
        if (code == 504) {
            onFailed(HttpErrorCode.ERROR, R.string.retry_later);
            return;
        }

        String message = error.message();
        if (code == HttpErrorCode.SERVER && !BuildConfig.DEBUG) {
            message = "";
        }
        onFailed(String.valueOf(code), message);
    }

    protected void onFailed(String code, String message) {
        onSafeFailed(code, message);
    }

}
